import Vue from 'vue'
import { Storage } from '../Storage'
//#ifdef H5
import Cookie from 'js-cookie'
//#endif

// Storage.setStorageSync('PHPSESSID', '');

const SESSID_NAME = 'PHPSESSID';
const TOKEN_NAME = 'token';
const USER_INFO_NAME = 'userInfo';

String.prototype.setPrefix = function () {
    const prefix = 'Login-';
    return `${prefix}${this}`
}

module.exports = {
    isExpires: false,
    refreshToken: false,
    refreshTokenUrl: null,
    // 是否刷新token
    set isRefreshToken (bol) {
        this.refreshToken = bol;
        Storage.setStorageSync('isRefreshToken'.setPrefix(), !!bol)
    },
    get isRefreshToken () {
        if (this.refreshToken === true) {
            Storage.setStorageSync('isRefreshToken'.setPrefix(), true);
            return this.refreshToken;
        }
        return Storage.getStorageSync('isRefreshToken'.setPrefix());
    },
    // 是否刷新用户信息
    set isRefreshUserInfo (bol) {
        this.refreshUserInfo = bol;
        Storage.setStorageSync('isRefreshUserInfo'.setPrefix(), !!bol)
    },
    get isRefreshUserInfo () {
        if (this.refreshUserInfo === true) {
            Storage.setStorageSync('isRefreshUserInfo'.setPrefix(), true);
            return this.refreshUserInfo;
        }
        return Storage.getStorageSync('isRefreshUserInfo'.setPrefix());
    },
    // 是否首次登录
    set isFirstLogin (bol) {
        Storage.setStorageSync('isFirstLogin'.setPrefix(), !!bol)
    },
    get isFirstLogin () {
        let isFirstLogin = Storage.getStorageSync('isFirstLogin'.setPrefix());
        if (typeof isFirstLogin !== 'boolean') {
            isFirstLogin = true;
            Storage.setStorageSync('isFirstLogin'.setPrefix(), true);
        }
        return isFirstLogin;
    },
    set sessionId (sessionId) {
        let expires = '1h'; //一小时
        if (!this.isExpires) expires = '7d'; //七天
        try {
            const a = Vue.$global_util.qs.parse2(sessionId);
            let session;
            if (typeof sessionId !== 'string') return;
            if (a.PHPSESSID && typeof a.PHPSESSID === 'string') {
                session = a.PHPSESSID
            } else {
                session = sessionId
            }
            if (session) {
                Storage.setStorageSync(SESSID_NAME, session, expires);
                //#ifdef H5
                Cookie.set(SESSID_NAME, session, Date.now() + 60 * 60 * 1000);
                //#endif
            }
        } catch (e) {

        }
    },
    get sessionId () {
        return Storage.getStorageSync(SESSID_NAME) || '';
    },
    set token (val) {
        let expires = '1h'; //一小时
        if (!this.isExpires) expires = '7d'; //七天
        if (typeof val === 'string') Storage.setStorageSync(TOKEN_NAME, val, expires)
    },
    get token () {
        return Storage.getStorageSync(TOKEN_NAME) || ''
    },
    set userInfo (obj) {
        Storage.setStorageSync(USER_INFO_NAME, obj)
    },
    get userInfo () {
        return Storage.getStorageSync(USER_INFO_NAME) || {}
    },
    setLogin ({token, sessionId, userInfo, isLogin}) {
        this.token = token;
        this.sessionId = sessionId;
        this.userInfo = userInfo;
        this.isFirstLogin = false;
    },
    getLogin () {
        let vm = this;
        return {
            token: vm.token,
            sessionId: vm.sessionId,
            userInfo: vm.userInfo
        }
    },
    removeLogin () {
        const data = this.getLogin();
        Object.keys(data).forEach(key => {
            Storage.removeStorageSync(key)
        });
        //#ifdef H5
        Cookie.remove(SESSID_NAME);
        //#endif
    },
    toLogin () {
        const {qs} = Vue.$global_util;
        const {isEmptyObject} = Vue.$global_util.jude;
        const pages = getCurrentPages();
        if (pages && pages.length > 0) {
            const page = pages[pages.length - 1];
            const {query} = page.$vm.__options;
            const q = isEmptyObject(query) ? '' : `?${qs.stringify(query)}`;
            const redirectUrl = `/${page.route}${q}`;
            Vue.$Storage.setStorageSync('redirectUrl', redirectUrl, '1d')
        }
        Vue.$$router.replace({
            url: Vue.$config.LOGIN_PATH,
            isToMiniProgram: true
        });
    }
};
